package weeny.face;

import java.awt.BorderLayout;
import java.awt.Component;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.JPanel;
import javax.swing.JTabbedPane;

/**
 * It is a simple interface manager. Just organize components in a tabbed pane.
 * And in each tab, it stack components together, and every component takes a
 * single line. 
 * 
 * Every tab in this tabbed pane has a button. When you click it, that tab will
 * be opened in a separated window so that you can move it around or resize it.
 * When you close the separated window, the tab will go back to tis tabbed pane.
 * 
 * @author weeny
 */
public class WidgetTabbedPane extends JTabbedPane{
	private Map<String, List> map = new HashMap<String, List>();
	
	/**
	 * Add another component to the north part of this pane
	 * @see #addWidget(String, Component, boolean)
	 */
	public void addWidget(String tabName, Component component) {
		addWidget(tabName, component, true);
	}
	/**
	 * Add a component to the north or south part of this pane, create a new tab
	 * if necessary (i.e., the tabName is new).
	 */
	public void addWidget(String tabName, Component component, boolean north) {
		List<Component> list = map.get(tabName);
		if (list == null) {
			list = new ArrayList<Component>();
			map.put(tabName, list);
			JPanel panel = new JPanel(new BorderLayout());
			panel.add(component, north? BorderLayout.NORTH:BorderLayout.SOUTH);
			list.add(component);
			this.add(tabName, panel);
			//we use the SeparatableTabComponent, so that we can click it to
			//open it in a separated window
			SeparatableTabComponent btc = new SeparatableTabComponent(this);
			int index = this.indexOfTab(tabName);
			this.setTabComponentAt(index, btc);
		}
		else{
			JPanel panel = new JPanel(new BorderLayout());
			panel.add(component, north? BorderLayout.NORTH:BorderLayout.SOUTH);
			JPanel parent = (JPanel) list.get(list.size()-1).getParent();
			parent.add(panel, BorderLayout.CENTER);
			map.get(tabName).add(component);
		}
	}
}